package com.projetoptymo;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * Classe qui permet de trouver la position GPS de l'utilisateur.
 * Il s'agit d'une classe Singleton
 * @version 0.1
 */
public class CurrentPosition
{
	// l'instance unique
	private static CurrentPosition instance = null;
	
	Location CurrentLocation;
	LocationManager objgps;
	Myobjlistener objlistener;
	
	// appel classique, impossible pour instancier a cause du param
	public final synchronized static CurrentPosition getInstance()
	{
        if (instance != null) 
        	return instance;
        return null;
    }
	
	// appel pour la construction de l'objet
	public final synchronized static CurrentPosition getInstance(Context cont)
	{
        if (instance == null) 
        	instance = new CurrentPosition(cont);
        return instance;
    }
	
	
	private CurrentPosition(Context cont)
	{
		objgps = (LocationManager)cont.getSystemService(Context.LOCATION_SERVICE);
		
		objlistener = new Myobjlistener();
		
		objgps.requestLocationUpdates( 
				LocationManager.GPS_PROVIDER, 
		        0,
		        0,
		        objlistener);
	}
	
	
	
	public Location getPosition()
	{
		Location loc = objgps.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		// test :
		//geo fix 6.834512 47.645517
		
		return loc;
	}
	
	
	private class Myobjlistener implements LocationListener 
	{ 
		public void onProviderDisabled(String provider)
		{} 
 
 
		public void onProviderEnabled(String provider)
		{} 
 
 
		public void onStatusChanged(String provider, int status, Bundle extras)
		{} 
 
 
		public void onLocationChanged(Location location)
		{
			CurrentLocation = location;
		} 
 
	}
 
}
